package com.example.sb.controller;

import com.example.sb.pojo.SexEnum;
import com.example.sb.pojo.User;
import com.example.sb.service.MybatisUserService;
import com.example.sb.service.UserBatchService;
import com.example.sb.validator.UserValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kedong.lin
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private MybatisUserService mybatisUserService;

    @Autowired
    private UserBatchService userBatchService;

    @InitBinder
    public void initBinder(WebDataBinder binder){
        binder.setValidator(new UserValidator());
    }

    @RequestMapping("/add")
    public String addUser(){
        User user = new User();
        user.setUserName("sd");
        user.setNote("ewwww");
        user.setSex(SexEnum.FEMAIL);
       // jpaUserRepository.save(user);
        return "success";
    }
    @RequestMapping("/getUser")
    public User getUserById(@RequestHeader("useShadow") boolean useShadow, long id){
        if(useShadow){
            System.out.println("accept head useShadow :"+useShadow);
        }else{
            System.out.println("accept head not useShadow :"+useShadow);
        }


       return mybatisUserService.getUser(id);
    }

    @RequestMapping("/save")
    public void saveUser(){
        User user = new User();
        user.setUserName("wer");
        user.setNote("test transation");
        mybatisUserService.saveUser(user);
    }
    @GetMapping("/saveConverter")
    @ResponseBody
    public List<User> saveConverterUser(@RequestParam("userList") List<User> userList){
        return userList;
    }
    @RequestMapping("/saveValidated")
    @ResponseBody
    public Map<String,String> saveValidated(@Valid User user, Errors errors){
        Map<String,String> map = new HashMap<>();
        if(errors.hasErrors()){
            List<ObjectError> allErrors = errors.getAllErrors();
            for (ObjectError error:allErrors) {
                if(error instanceof FieldError){
                    FieldError fieldError=(FieldError)error;
                    map.put(fieldError.getField(),fieldError.getDefaultMessage());
                }else{
                    map.put(error.getObjectName(),error.getDefaultMessage());
                }
            }

        }
        return map;
    }
    @RequestMapping("/saves")
    public void saveUserList(){
        User user = new User();
        user.setUserName("wer");
        user.setNote("test transaction");
        User user2 = new User();
        user2.setUserName("wetw");
        user2.setNote("test transaction2");
        List<User> users = new ArrayList<>();
        users.add(user);
        users.add(user2);
        userBatchService.batchSaveUser(users);
    }

    @GetMapping("/param")
    public String testParam(String[] strs,Integer[] integers){
        System.out.println(strs);
        System.out.println(integers);
        return "success";
    }
}
